package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.McAccountInfoMapper;
import cc.rengu.igas.momp.common.entity.TMAccountInfo;
import cc.rengu.igas.momp.common.entity.TMMcUserInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author Jinan Liu
 * @date 2020/3/26 00:21
 * @description 营销活动机构规则数据库操作接口实现
 */
public class McAccountInfoMapperImpl implements McAccountInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    @Override
    public int insertMcAccountInfo(TMAccountInfo tmAccountInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMcAccountInfo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmAccountInfo, TMAccountInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入法人机构号<{}>用户标识<{}>的收银员信息失败!<{}>", tmAccountInfo.getInstId(), tmAccountInfo.getUserId(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 使用主键查询收银员信息表（机构号，用户标识，账号）
     *
     * @param instId
     * @param userId
     * @param acctNo
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/30
     */
    @Override
    public TMAccountInfo selectAccountByUserIdAcctNo(String instId, String userId, String acctNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcAccountInfo");

        TMAccountInfo tmAccountInfo = new TMAccountInfo();
        tmAccountInfo.setInstId(instId);
        tmAccountInfo.setUserId(userId);
//        tmAccountInfo.setUserAcct(acctNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmAccountInfo, tmAccountInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件获取收银员信息表失败!<{}>", iReturnCode);
            return null;
        }
        return tmAccountInfo;
    }

    /**
     * 使用主键查询收银员信息表（机构号，用户标识）
     *
     * @param instId
     * @param userId
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/30
     */
    @Override
    public TMAccountInfo selectMcAccountInfoByInstIdUserId(String instId, String userId) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcAccountInfoByInstIdUserId");

        TMAccountInfo tmAccountInfo = new TMAccountInfo();
        tmAccountInfo.setInstId(instId);
        tmAccountInfo.setUserId(userId);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmAccountInfo, tmAccountInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件获取收银员信息表失败!<{}>", iReturnCode);
            return null;
        }
        return tmAccountInfo;
    }

    /**
     * 使用主键查询营销活动用户信息表（机构号,营销活动编号，账号）
     *
     * @param instId
     * @param mcNo
     * @param acctNo
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/30
     */
    @Override
    public TMMcUserInfo selcetMcMchntInfoByInstIdMcNoAcctNo(String instId, String mcNo, String acctNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selcetMcMchntInfoByInstIdMcNoAcctNo");

        TMMcUserInfo tmMcUserInfo = new TMMcUserInfo();
        tmMcUserInfo.setInstId(instId);
        tmMcUserInfo.setMcNo(mcNo);
        tmMcUserInfo.setAcctNo(acctNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcUserInfo, tmMcUserInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件获取营销活动用户信息表失败!<{}>", iReturnCode);
            return null;
        }
        return tmMcUserInfo;
    }

    @Override
    public int insertMcUserInfo(TMMcUserInfo tmMcUserInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMcUserInfo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcUserInfo, TMMcUserInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入法人机构号<{}>营销活动编号<{}>的营销活动用户信息失败!<{}>", tmMcUserInfo.getInstId(), tmMcUserInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    @Override
    public int updateMcAccountInfo(TMAccountInfo tmAccountInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcAccountInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmAccountInfo, TMAccountInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("用户账号<{}>更新收银员表业务状态失败! RETURN_CODE=<{}>", tmAccountInfo.getUserAcct(), iReturnCode);
            return iReturnCode;
        }
        return Database.DBS_SUCCESS;
    }
}
